<?php
/**
 * Created by PhpStorm.
 * User: ysk
 * Date: 2021/09/14
 * Time: 15:54
 */
namespace app\test\controller;

use app\test\model\OcrMatch;
use think\Db;

class Test {


    public function getUser(){
        $user = Db::table('app_auth')->where('id','=','1')->select();


        var_dump($user);
    }




    public function test(){
        $word = '[{"words":"\u4e0a\u53489:2310.5K\/s","location":{"left":82,"top":32,"height":31,"weight":292}},{"words":"115","location":{"left":938,"top":28,"height":36,"weight":57}},{"words":"\u8f6c\u8d26\u8be6\u60c5","location":{"left":436,"top":117,"height":49,"weight":203}},{"words":"\u5317\u4eac\u957f\u6548\u6295\u8d44\u7ba1\u7406\u6709\u9650\u516c\u53f8","location":{"left":215,"top":292,"height":40,"weight":641}},{"words":"\u4e00\u5e73\u5b89\u94f6\u884c\uff08\u539f\u6df1\u5733\u53d1\u5c55\u94f6\u884c\uff09\uff080045\uff09","location":{"left":200,"top":383,"height":39,"weight":682}},{"words":"\uffe5100,000.00","location":{"left":294,"top":512,"height":63,"weight":480}},{"words":"\u4ea4\u6613\u65f6\u95f4","location":{"left":63,"top":709,"height":49,"weight":173}},{"words":"2021\/09\/2409:22:35","location":{"left":608,"top":717,"height":36,"weight":402}},{"words":"\u4ea4\u6613\u7c7b\u578b","location":{"left":65,"top":807,"height":41,"weight":166}},{"words":"\u5411\u4ed6\u884c\u8d26\u6237\u8f6c\u8d26","location":{"left":735,"top":807,"height":41,"weight":277}},{"words":"\u4ea4\u6613\u6e20\u9053","location":{"left":65,"top":897,"height":41,"weight":167}},{"words":"\u624b\u673a\u94f6\u884c","location":{"left":856,"top":897,"height":41,"weight":156}},{"words":"\u4ed8\u6b3e\u8d26\u6237","location":{"left":63,"top":985,"height":49,"weight":168}},{"words":"6236***7558","location":{"left":771,"top":990,"height":36,"weight":239}},{"words":"\u51ed\u8bc1\u53f7","location":{"left":63,"top":1075,"height":51,"weight":127}},{"words":"144563074030","location":{"left":740,"top":1077,"height":39,"weight":270}},{"words":"\u67e5\u770b\u548cTA\u7684\u8f6c\u8d26\u8bb0\u5f55","location":{"left":67,"top":1241,"height":31,"weight":369}},{"words":"\u6e29\u99a8\u63d0\u793a","location":{"left":68,"top":1382,"height":32,"weight":139}},{"words":"\u6b64\u9875\u9762\u8bf4\u660e\u8f6c\u8d26\u4ea4\u6613\u53d7\u7406\u6210\u529f\uff0c\u4e0d\u4ee3\u8868\u8d44\u91d1\u5df2\u8f6c\u5165\u6536\u6b3e\u8d26","location":{"left":68,"top":1458,"height":31,"weight":906}},{"words":"\u6237\uff0c\u4e0d\u4f5c\u4e3a\u6536\u4ed8\u65b9\u4ea4\u6613\u786e\u8ba4\u7684\u6700\u7ec8\u4f9d\u636e\u3002\u4ea4\u6613\u7ed3\u679c\u8bf7\u4ee5\u6536\u6b3e","location":{"left":70,"top":1531,"height":31,"weight":930}},{"words":"\u8d26\u6237\u5b9e\u9645\u5165\u8d26\u4e3a\u51c6","location":{"left":68,"top":1604,"height":32,"weight":301}},{"words":"\u6536\u8d77\u5165","location":{"left":897,"top":1675,"height":41,"weight":115}},{"words":"\u518d\u6b21\u8f6c\u8d26","location":{"left":190,"top":1894,"height":51,"weight":201}},{"words":"\u7535\u5b50\u56de\u5355","location":{"left":687,"top":1896,"height":52,"weight":200}},{"words":"\u4e09","location":{"left":299,"top":2255,"height":48,"weight":36}}]';
        $words = array_column(json_decode($word,true),'words');
        $date_pattern = $preg = '/[2][0][2]\d{1}\-\d{1,2}\-\d{1,2}|[2][0][2]\d{1}\/\d{1,2}\/\d{1,2}|[2][0][2]\d{1}年\d{1,2}月\d{1,2}日|[2][0][2]\d{1}\.\d{1,2}\.\d{1,2}|^[2][0][2]\d{1}\d{1,2}\d{1,2}\ *\d{1,2}\:+/';
        var_dump($words);
        $words[7] = '20210927          15:dfsf:';
        foreach ( $words as $k=>$v) {
            preg_match($date_pattern, $v, $matche);
            if (!empty($matche)) {
                $matches = $matche;
            }
        }
        if(strpos($matches[0],':')){
            $match_date = substr($matches[0],0,8);
        }
        var_dump(date('Y-m-d', strtotime($match_date)));die;

        $text = join('',array_column(json_decode($word,true),'words'));

        $r = preg_replace('/([\x80-\xff])/i','',$text);
        $r = preg_replace('/([A-Za-z]\/)/i','',$r);

        var_dump($r);die;
    }

    public function similar()
    {
//        echo 'similar'; die;
        $ocr = new OcrMatch();
//        $ocr_res = Db::table('ocr_attach')->where('id','=',1)->select();
        $ocr_res = Db::table('ocr_attach')->where('id','=',844)->select();

        foreach ($ocr_res as $key=>$val){
            $ocr_data = array_column(json_decode($val['ocr_data'],true),'words');
//            var_dump($ocr_data);
            $db_data = [
                'payer'=>$val['customer_name'],
                'paying_bank'=>$val['bank_name'],
                'paying_bank_number'=>$val['bank_card'],
                'payee'=>$val['open_bank'],
                'due_bank_number'=>$val['raise_account'],
                'due_bank'=>$val['raise_username'],
                'pate_date' => date('Y-m-d',$val['signup_time'])
            ];
            $res[] = $ocr->get($db_data,$ocr_data);

        }

//        //银行卡
//        $card_code = ['raise_account' => $db_data['due_bank_number'], 'bank_card' =>$db_data['paying_bank_number'] ];
//        $str = $res[0]['ocr_data'];
//        foreach ($card_code as $key => $val) {
//            // 银行卡号完全匹配
//            if (strstr($str, $val)) {
//                $match_result[$key] = $val;
//
//            } else {
//                //前四后四匹配
//                $card_pre_4 = substr($val, 0, 4);
//                $card_end_4 = substr($val, -4);
//                //前四和后四出现的位置
//                $card_pre_4_pos = strpos($str, $card_pre_4);
//                $card_end_4_pos = strpos($str, $card_end_4);
//                if ($card_pre_4_pos && $card_end_4_pos) {  //前四后四都存在
//                    $match_result[$key] = substr($str, $card_pre_4_pos, $card_end_4_pos - $card_pre_4_pos + 4);
//
//                } else {
//                    //前二后二匹配
//                    $card_pre_2 = substr($val, 0, 2);
//                    $card_end_2 = substr($val, -2);
//
//                    var_dump($card_pre_2,$card_end_2);
//                    //前二和后二出现的位置
//                    $card_pre_2_pos = strpos($str, $card_pre_2);
//                    $card_end_2_pos = strpos($str, $card_end_2);
//                    if ($card_pre_2_pos && $card_end_2_pos) {  //前二后二都存在
//                        $match_result[$key] = substr($str, $card_pre_2_pos, $card_end_2_pos - $card_pre_2_pos + 2);
//
//                    }
//                }
//            }
//
//        }
//
//        $raise_account = preg_replace('/([\x80-\xff])/i','',$match_result['raise_account']);
//
//var_dump($raise_account);




        var_dump($res);die;

        $data = [];
        foreach ($res as $k=>$v){


            $str = $v['ocr_data'] ;

            if(empty($data[$k])){
                $data[$k] = '';
            }

            //时间
            if($v['pay_date']==$v['db_data']['pate_date']) {
                $data[$k] .= '+';
            }else{
                $data[$k] .= '-';
            }

            //金额

            //汇款方卡号
            if ($v['db_data']['paying_bank_number'] == $v['paying_bank_number']) {
                $data[$k] .= '+';
            } else {
                //前四后四匹配
                $card_pre_4 = substr($v['db_data']['paying_bank_number'], 0, 4);
                $card_end_4 = substr($v['db_data']['paying_bank_number'], -4);
//                var_dump($card_pre_4,$card_end_4);
                //前四和后四出现的位置
                $card_pre_4_pos = strpos($str, $card_pre_4);
                $card_end_4_pos = strpos($str, $card_end_4);
                if ($card_pre_4_pos && $card_end_4_pos) {  //前四后四都存在
                    $v['paying_bank_number'] = substr($str, $card_pre_4_pos, $card_end_4_pos - $card_pre_4_pos + 4);
                    $data[$k] .= '+';
                } else {
                    //前二后二匹配
                    $card_pre_2 = substr($v['db_data']['paying_bank_number'], 0, 2);
                    $card_end_2 = substr($v['db_data']['paying_bank_number'], -2);
                    //前二和后二出现的位置
                    $card_pre_2_pos = strpos($str, $card_pre_2);
                    $card_end_2_pos = strpos($str, $card_end_2);
//                    var_dump($card_pre_2,$card_end_2);
                    if ($card_pre_2_pos && $card_end_2_pos) {  //前二后二都存在
                        $v['paying_bank_number'] = substr($str, $card_pre_2_pos, $card_end_2_pos - $card_pre_2_pos + 2);
                        $data[$k] .= '+';
                    }else{
                        $data[$k] .= '-';
                    }
                }
            }

            //收款方开户行
            if($v['payee']==$v['db_data']['payee']){
                $data[$k] .= '+';
            }else {
                $pos = mb_strpos($v['payee'],'银行');
                $sub_ban_name = mb_substr($v['payee'],0,$pos+2);
                if($pos>0 && (strstr($sub_ban_name,$v['db_data']['payee']) || strstr($v['db_data']['payee'],$sub_ban_name))){
                    $data[$k] .= '+';
                }else{
                    //反向从cor找银行子串
                    $open_bank_pos = mb_strpos($v['db_data']['payee'],'银行');
                    $sub_open_bank = mb_substr($v['db_data']['payee'],0,$open_bank_pos+2);
                    if (strstr($str, $sub_open_bank)) {
                        $data[$k] .= '+';
                        $match_result['bank_name'] = $sub_open_bank;
                    }else {
                        $data[$k] .= '-';
                    }
                }
            }

//            //姓名
//            if($v['payer'] == $v['db_data']['payer']){
//                $data[$k] .= '+';
//            }else{
//                $data[$k] .= '-';
//            }

            //收款方户名
//            if(strstr($v['due_bank'] ,$v['db_data']['due_bank']) || strstr($v['db_data']['due_bank'],$v['due_bank'])){
            if(strstr($str, $v['db_data']['due_bank'])){
                $data[$k] .= '+';
            }else{
                $data[$k] .= '-';
            }

            //收款方账号
            if ($v['due_bank_number'] == $v['db_data']['due_bank_number']){
                $data[$k] .= '+';
            } else {
                //前四后四匹配
                $card_pre_4 = substr($v['db_data']['due_bank_number'], 0, 4);
                $card_end_4 = substr($v['db_data']['due_bank_number'], -4);
                //前四和后四出现的位置
                $card_pre_4_pos = strpos($str, $card_pre_4);
                $card_end_4_pos = strpos($str, $card_end_4);
                if ($card_pre_4_pos && $card_end_4_pos) {  //前四后四都存在
                    $v['due_bank_number'] = substr($str, $card_pre_4_pos, $card_end_4_pos - $card_pre_4_pos + 4);
                    $data[$k] .= '+';
                } else {
                    //前二后二匹配
                    $card_pre_2 = substr($v['db_data']['due_bank_number'], 0, 2);
                    $card_end_2 = substr($v['db_data']['due_bank_number'], -2);
                    //前二和后二出现的位置
                    $card_pre_2_pos = strpos($str, $card_pre_2);
                    $card_end_2_pos = strpos($str, $card_end_2);
                    if ($card_pre_2_pos && $card_end_2_pos) {  //前二后二都存在
                        $v['due_bank_number'] = substr($str, $card_pre_2_pos, $card_end_2_pos - $card_pre_2_pos + 2);
                        $data[$k] .= '+';
                    }else{
                        $data[$k] .= '-';
                    }
                }
            }
        }
//        var_dump($res,$data);
//        echo(implode(',',$data));
        $count = 0;

        for ($t=0;$t<count($data);$t++){
            if($data[$t] == '+++++'){
                $count++;
            }
        }

        var_dump($count);


    }


    public function preg()
    {
//        echo 'preg'; die;
        $ocr = new OcrMatch();
        $ocr_res = Db::table('ocr_attach')->where('id', '<', 1000)->select();

        foreach ($ocr_res as $key => $val) {
            $match_result = json_decode($val['match_result'], true);
            $ocr_data = json_decode($val['ocr_data'],true);
//            var_dump($match_result);
            $db_data = [
                'payer' => $val['customer_name'],
                'paying_bank' => $val['bank_name'],
                'paying_bank_number' => $val['bank_card'],
                'payee' => $val['open_bank'],
                'due_bank_number' => $val['raise_account'],
                'due_bank' => $val['raise_username']
            ];
//            var_dump($db_data);
            $res[] = $ocr->preg($db_data,$match_result,$ocr_data);
        }
        echo(implode(',',$res));

    }


    public function strstr()
    {
        echo 'strstr'; die;
    }

}
